//
//  ProjectDeatailViewController.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/3.
//  Copyright © 2018年 王右. All rights reserved.
//

#import "ProjectDeatailViewController.h"
#import "ImagesAndContentTableViewCell.h"
#import "ImagesWithOutContentTableViewCell.h"
#import "MonthUpdateViewController.h"
#import "ViewDynamicViewController.h"
#import "WMPageController.h"
#import "ProjectDetailModel.h"
#import "AttachListModel.h"
#import "AddNewProjectController.h"
#import "ProjectDyListModel.h"

@interface ProjectDeatailViewController ()<UITableViewDelegate,UITableViewDataSource,WMPageControllerDataSource>

@property (weak, nonatomic) IBOutlet UITableView *baseTableView;

@property (strong, nonatomic) IBOutlet UITableViewCell *firstCell;
@property (weak, nonatomic) IBOutlet UILabel *projectCategoryLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectLocationLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectSizeLabel;
@property (weak, nonatomic) IBOutlet UIButton *viewDynamicButton;
@property (weak, nonatomic) IBOutlet UILabel *allInvestmentMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectNameLabel;

@property (strong, nonatomic) IBOutlet UITableViewCell *secondCell;
@property (weak, nonatomic) IBOutlet UILabel *buildCompanyLabel;
@property (weak, nonatomic) IBOutlet UILabel *daiJianCompanyLabel;
@property (weak, nonatomic) IBOutlet UILabel *shiGongCompanyLabel;
@property (weak, nonatomic) IBOutlet UILabel *observerCompanyLabel;
@property (weak, nonatomic) IBOutlet UILabel *desinCompanyLabel;
@property (weak, nonatomic) IBOutlet UILabel *reviewComanyLabel;

@property (strong, nonatomic) IBOutlet UITableViewCell *thirdCell;
@property (weak, nonatomic) IBOutlet UILabel *projectTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *roadLevelLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectStartTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *projectEndTimeLabel;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *roadLevelHeight;

@property (strong, nonatomic) IBOutlet UITableViewCell *fourCell;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (nonatomic, strong) ImagesAndContentTableViewCell *fiveCell;

@property (nonatomic, strong) ImagesWithOutContentTableViewCell *sixCell;

@property (nonatomic, strong) ImagesAndContentTableViewCell *sevenCell;

@property (nonatomic, strong) ImagesAndContentTableViewCell *eightCell;

@property (strong, nonatomic) IBOutlet UITableViewCell *nineCell;

@property (nonatomic, strong) NSArray *cellArr;

@property (nonatomic, strong) ProjectDetailModel *infoModel;

@property (nonatomic, strong) NSMutableArray *dyModelArr;
@end

@implementation ProjectDeatailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"项目详情";
    
    _viewDynamicButton.layer.cornerRadius = 2;
    _viewDynamicButton.layer.masksToBounds = YES;
    
    _fiveCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];
    
    _sixCell = (ImagesWithOutContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesWithOutContentTableViewCell"];
    
    _sevenCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];
    
    _eightCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];
    
    _cellArr = @[_fiveCell,_secondCell,_thirdCell,_fourCell,_fiveCell,_sixCell,_sevenCell,_eightCell,_nineCell];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getDetailData];

}

- (void)getAttachData{
    if (_projectID.length == 0) {
        [self.view makeToast:@"项目ID为空,可能项目已经不存在!"];
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self hudWithText:@"加载数据中..."];
    
    [RequestManager postRequestWithModule:@"/v1/attachlist" param:@{@"account":User_Account,@"token":User_SignToken,@"pmid":_projectID} model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSArray *picArr = [AttachListModel objectWithArray:obj[@"result"][@"atlist"]];
        NSLog(@"%@",obj);
        [self configPicArrWithPicArr:picArr];
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}

- (void)configPicArrWithPicArr:(NSArray *)picArr{
    
    NSMutableArray *zbPicArr = [NSMutableArray array];
    __block NSString *zbContent;
    NSMutableArray *fgPicArr = [NSMutableArray array];
    NSMutableArray *gtPicArr = [NSMutableArray array];
    NSMutableArray *ghPicArr = [NSMutableArray array];
    NSMutableArray *hbPicArr = [NSMutableArray array];
    NSMutableArray *zjPicArr = [NSMutableArray array];
    NSMutableArray *qtPicArr = [NSMutableArray array];
    NSMutableArray *bzPicArr = [NSMutableArray array];
    __block NSString *baojianContent;
    __block NSString *qtContent;
    __block NSString *noteContent;
    
    [picArr enumerateObjectsUsingBlock:^(AttachListModel  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.attachtype isEqualToString:@"zb"]) {
            [zbPicArr addObject:obj.attachurl];
            zbContent = obj.attachdesc;
        }else if ([obj.attachtype isEqualToString:@"fg"]){
            [fgPicArr addObject:obj.attachurl];
        }else if ([obj.attachtype isEqualToString:@"gt"]){
            [gtPicArr addObject:obj.attachurl];
        }else if ([obj.attachtype isEqualToString:@"gh"]){
            [ghPicArr addObject:obj.attachurl];
        }else if ([obj.attachtype isEqualToString:@"hb"]){
            [hbPicArr addObject:obj.attachurl];
        }else if ([obj.attachtype isEqualToString:@"zj"]){
            [zjPicArr addObject:obj.attachurl];
            baojianContent = obj.attachdesc;
        }else if ([obj.attachtype isEqualToString:@"qt"]){
            [qtPicArr addObject:obj.attachurl];
            qtContent = obj.attachdesc;
        }else{
            [bzPicArr addObject:obj.attachurl];
            noteContent = obj.attachdesc;
        }
    }];
    _infoModel.zbImageArr = zbPicArr;
    _infoModel.fgImageArr = fgPicArr;
    _infoModel.gtImageArr = gtPicArr;
    _infoModel.ghImageArr = ghPicArr;
    _infoModel.hbImageArr = hbPicArr;
    _infoModel.zjImageArr = zjPicArr;
    _infoModel.qtImageArr = qtPicArr;
    
    _infoModel.bzConent = noteContent;
    _infoModel.bzImageArr = bzPicArr;
    
    [self configTableView];
}

- (void)configTableView{
    [_baseTableView reloadData];
}

- (void)getDetailData{
    if (_projectID.length == 0) {
        [self.view makeToast:@"项目ID为空,可能项目已经不存在!"];
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    [self hudWithText:@"加载数据中..."];
    [RequestManager postRequestWithModule:@"v1/getpminfo" param:@{@"account":User_Account,@"token":User_SignToken,@"pmid":_projectID} model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSLog(@"%@",obj);
        NSDictionary *dic = obj[@"result"];
        ProjectDetailModel *model = [ProjectDetailModel objectWithDictionary:dic[@"pminfo"]];
        self->_infoModel = model;
        [self configInfo];
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}
- (void)configInfo{
    //项目类别
    switch (_infoModel.pmtype.integerValue) {
        case 1:
            _projectCategoryLabel.text = @"市政道路";
            break;
        case 2:
            _projectCategoryLabel.text = @"居住建筑";
            break;
        case 3:
            _projectCategoryLabel.text = @"公共配套";
            break;
        default:
            _projectCategoryLabel.text = @"其他";
            break;
    }
    //项目位置
    _projectLocationLabel.text = _infoModel.pmposition;
    //项目类别
    if ([_infoModel.pmgm isEqualToString:@"A"]) {
        _projectSizeLabel.text = @"大型";
    }else if ([_infoModel.pmgm isEqualToString:@"B"]){
        _projectSizeLabel.text = @"中型";
    }else{
        _projectSizeLabel.text = @"小型";
    }
    //计划总投资
    if (_infoModel.pmjhztz.floatValue > 10000) {
        _allInvestmentMoneyLabel.text = [NSString stringWithFormat:@"%.2f万",_infoModel.pmjhztz.floatValue / 10000];
    }else{
        _allInvestmentMoneyLabel.text = _infoModel.pmjhztz;
    }
    //项目名称
    _projectNameLabel.text = _infoModel.projectname;
    //建设单位
    _buildCompanyLabel.text = _infoModel.pmjsdw;
    //代建单位
    _daiJianCompanyLabel.text = _infoModel.pmdjdw;
    //施工单位
    _shiGongCompanyLabel.text = _infoModel.pmsgdw;
    //监理单位
    _observerCompanyLabel.text = _infoModel.pmjldw;
    //设计单位
    _desinCompanyLabel.text = _infoModel.pmsjdw;
    //勘察单位
    _reviewComanyLabel.text = _infoModel.pmkcdw;
    //建设年限
    _projectTimeLabel.text = _infoModel.pmjslx;
    //道路等级
    _roadLevelLabel.text = _infoModel.pmroadgrade;
    //开工时间
    _projectStartTimeLabel.text = [_infoModel.pmstartday retureTimeStringWithFormate:@"yyyy-MM-dd"];
    //交付时间
    _projectEndTimeLabel.text = [_infoModel.pmdeliverday retureTimeStringWithFormate:@"yyyy-MM-dd"];
    //棚改情况
    _contentLabel.text = _infoModel.pmpgrzqk;

    [self getAttachData];
}



- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_infoModel) {
        return _cellArr.count;
    }else{
       return 0;
    }
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return 240;
            break;
        case 1:
            return 200;
            break;
        case 2:
            if (_infoModel.pmtype.integerValue == 1) {
                _roadLevelHeight.constant = 30;
                return 140;
            }else{
                _roadLevelHeight.constant = 0;
                return 110;
            }
            return 140;
            break;
        case 3:{
            if (_infoModel.pmtype.integerValue==2) {
                CGFloat contentHeight = [self returnContentTextHeightWithText:_infoModel.pmpgrzqk withFont:_contentLabel.font];
                return 60 + contentHeight;
            }
            return 0;
            break;
        }
        case 4:
            return [_fiveCell returnCellHeight];
            break;
        case 5:
            return [_sixCell returnCellHeight];
            break;
        case 6:
            return [_sevenCell returnCellHeight];
            break;
        case 7:
            return [_eightCell returnCellHeight];
            break;
        default:
            return 50;
            break;
    }
}

- (CGFloat )returnContentTextHeightWithText:(NSString *)content withFont:(UIFont *)font{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-25, NSIntegerMax)];
    label.text = content;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGSize size = label.bounds.size;
    return size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return _firstCell;
            break;
        case 1:
            return _secondCell;
            break;
        case 2:
            return _thirdCell;
            break;
        case 3:
            return _fourCell;
            break;
        case 4:
            _fiveCell.imageArr = _infoModel.zbImageArr;
            _fiveCell.textViewText = _infoModel.jjzbdesc.length==0?@"暂无内容":_infoModel.jjzbdesc;
            _fiveCell.nameLabel.text = @"经济技术指标";
            _fiveCell.canEdit = NO;
            return _fiveCell;
            break;
        case 5:
            _sixCell.nameLabel.text = @"报建手续";
            
            _sixCell.fagaiImageArr = _infoModel.fgImageArr;
            _sixCell.guotuImageArr = _infoModel.gtImageArr;
            _sixCell.guihuaImageArr = _infoModel.ghImageArr;
            _sixCell.huanbaoImageArr = _infoModel.hbImageArr;
            _sixCell.zhujianImageArr = _infoModel.zjImageArr;
            
            _sixCell.fagaiTextViewText = _infoModel.fgdesc.length==0?@"发改暂无内容":_infoModel.fgdesc;
            _sixCell.guoTuTextViewText = _infoModel.gtdesc.length==0?@"国土暂无内容":_infoModel.gtdesc;
            _sixCell.zhuJianTextViewText = _infoModel.zjdesc.length==0?@"住建暂无内容":_infoModel.zjdesc;
            _sixCell.huanBaoTextViewText = _infoModel.hbdesc.length==0?@"环保暂无内容":_infoModel.hbdesc;
            _sixCell.guiHuaTextViewText = _infoModel.ghdesc.length==0?@"规划暂无内容":_infoModel.ghdesc;

            _sixCell.type = BaoJianShouXuCellTypeFaGai;
            

            _sixCell.canEdit = NO;
            return _sixCell;
            break;
        case 6:
            _sevenCell.imageArr = _infoModel.qtImageArr;
            _sevenCell.textViewText = _infoModel.qtdesc.length==0?@"暂无内容":_infoModel.qtdesc;
            _sevenCell.nameLabel.text = @"其他附件";
            _sevenCell.canEdit = NO;
            return _sevenCell;
            break;
        case 7:
            _eightCell.imageArr = _infoModel.bzImageArr;
            _eightCell.textViewText = _infoModel.pmdesc.length==0?@"暂无内容":_infoModel.pmdesc;
            _eightCell.nameLabel.text = @"备注说明";
            _eightCell.canEdit = NO;
            return _eightCell;
            break;
        default:
            return _nineCell;
            break;
    }
}

- (IBAction)baseInfoEditAction:(id)sender {
    if([[LoginUserManager sharedManager] isCreater:_infoModel.createby]){
        AddNewProjectController *vc = [AddNewProjectController new];
        vc.isUpdateProjectInfo = YES;
        vc.projectDetailModel = _infoModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self.view makeToast:@"你没有权限修改此项目基础信息"];
    }
}
- (IBAction)viewDynamicButtonAction:(UIButton *)sender {
    [self getpmdylist];
//    [self.navigationController pushViewController:[self getPages] animated:YES];
}

#pragma mark 返回一个WMPageController对象
- (WMPageController *) getPages {
    //用上面两个数组初始化WMPageController对象
    WMPageController *pageController = [[WMPageController alloc] init];
    //设置WMPageController每个标题的宽度
    pageController.menuItemWidth = 100;
    pageController.menuBGColor = [UIColor whiteColor];
    //设置WMPageController标题栏的高度
    pageController.menuHeight = 50;
    pageController.titleSizeNormal = 12;
    pageController.menuViewBottomSpace = 10;
    pageController.pageAnimatable = NO;
    pageController.titleSizeSelected = 14;
    pageController.menuViewStyle = WMMenuViewStyleLine;
    pageController.titleColorSelected = [UIColor colorWithHexString:@"#74D18C"];
    pageController.titleColorNormal = [UIColor colorWithHexString:@"#777777"];
    pageController.menuView.lineColor = [UIColor colorWithHexString:@"#74D18C"];
    pageController.progressViewWidths = @[@(30),@(30)];
    pageController.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    //设置WMPageController选中的标题的颜色
    pageController.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    pageController.title = @"月度更新";
    pageController.dataSource = self;
    [pageController reloadData];
    return pageController;
}

- ( UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index{
    MonthUpdateViewController *vc = [MonthUpdateViewController new];
    vc.dyModel = _dyModelArr[index];
    vc.projectID = _projectID;
    vc.isUpdate = NO;
    return vc;
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController{
    return _dyModelArr.count;
}
- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index{
    NSMutableArray *titles = [NSMutableArray array];
    [_dyModelArr enumerateObjectsUsingBlock:^(ProjectDyListModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { 
        [titles addObject:obj.dymonths];
    }];
    return titles[index];
}

- (IBAction)projectUpdateAction:(id)sender {
    if([[LoginUserManager sharedManager] isCreater:_infoModel.createby]){
        MonthUpdateViewController *vc = [MonthUpdateViewController new];
        vc.isUpdate = YES;
        vc.projectID = _projectID;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self.view makeToast:@"您不是项目创建者,无法发布项目动态!"];
    }
    
}

- (void)getpmdylist{
    [self hudWithText:@"获取项目动态信息中..."];
    [RequestManager postRequestWithModule:@"v1/getpmdylist" param:@{@"account":User_Account,@"token":User_SignToken,@"pmid":_projectID} model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSDictionary *dic = obj[@"result"];
        NSArray *pmdyinfoarr = dic[@"pmdyinfo"];
        if (pmdyinfoarr.count == 0) {
            [self.view makeToast:@"当前项目还没有动态信息,请在底部点击项目更新进行更新"];
            return ;
        }else{
            self->_dyModelArr = [ProjectDyListModel objectWithArray:pmdyinfoarr];
            WMPageController *page = [self getPages];
            if (@available(iOS 11, *)) {
                page.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            }else{
                page.edgesForExtendedLayout = UIRectEdgeNone;
            }
            [self.navigationController pushViewController:page animated:YES];
        }
    } failure:^(NSString *error) {
        [self removeHud];
        [self hudWithText:error];
    }];
}


@end
